<template>
    <jb-crud-page
        ref="postPage"
        title-icon="carbon:batch-job"
        title-text="岗位管理"
        search-url="/api/admin/post/datas"
        :search-conditions="pageConditions"
        :edit-component="PostEdit"
    >
        <template #conditions-form>
            <n-input
                v-model:value="pageConditions.keywords"
                type="text"
                placeholder="输入关键字搜索"
                @keyup.enter="postPage?.loadData(true)"
            />
        </template>
        <template #opt>
            <n-button-group>
                <jb-btn
                    ghost
                    type="primary"
                    :icon="Icons.ADD"
                    @click="postPage?.showEditModal('新增岗位')"
                >
                    新增
                </jb-btn>
            </n-button-group>
        </template>
        <template #default="{ list, tableStartIndex }">
            <jb-table :start-index="tableStartIndex" :data="list">
                >
                <jb-column type="seq" title="序号" width="60" fixed="left"></jb-column>
                <jb-column
                    field="name"
                    title="岗位名称"
                    min-width="140"
                    fixed="left"
                ></jb-column>
                <jb-column
                    field="typeName"
                    title="岗位类型"
                    width="120"
                    fixed="left"
                ></jb-column>
                <jb-column field="sn" title="编码" min-width="120"></jb-column>
                <jb-column field="remark" title="备注" min-width="160"></jb-column>
                <jb-column field="enable" title="是否启用" width="80" fixed="right">
                    <template #default="{ row }">
                        <jb-switch
                            v-model:value="row.enable"
                            :url="`/api/admin/post/toggleEnable/${row.id}`"
                        ></jb-switch>
                    </template>
                </jb-column>
                <jb-column title="操作" width="110" fixed="right">
                    <template #default="{ row }">
                        <jb-btn
                            tip-text="编辑"
                            :icon="Icons.EDIT"
                            type="warning"
                            secondary
                            circle
                            @click="
                                postPage?.showEditModal('编辑岗位', {
                                    id: row.id
                                })
                            "
                        ></jb-btn>
                        <jb-btn
                            tip-text="删除"
                            :icon="Icons.DELETE"
                            secondary
                            type="error"
                            class="mx-8px"
                            circle
                            confirm-text="确定删除这条数据？"
                            :url="`/api/admin/post/delete/${row.id}`"
                            @success="postPage?.loadData()"
                        ></jb-btn>
                    </template>
                </jb-column>
            </jb-table>
        </template>
    </jb-crud-page>
</template>

<script setup lang="ts">
import { ref } from 'vue'
import { Icons } from '@/constants'
import { useResetableData } from '@/hooks/common/use-resetable-data'
import JbCrudPage from '@/components/_builtin/jb-crud-page/index.vue'
import PostEdit from './components/post-edit/index.vue'

const postPage = ref<InstanceType<typeof JbCrudPage> | null>(null)
const pageConditions = useResetableData({
    keywords: ''
})
</script>

<style scoped></style>
